//
//  ViewController.swift
//  LoadBSBJ
//
//  Created by eport_user1 on 2018/1/8.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
   
    let iden = "cell_iden"
    
    var tableview:UITableView?
    
    var a:String = "list"
    var page:Int = 0
    var type:Int = 29
     var maxTime: String = ""
    
    var mutaArray = [ContentModel]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //初始化界面
        setupNav()
        setupTable()
        
        //加载网络数据
        loadServerData()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController {
    
    func setupNav() -> Void {
        
        view.backgroundColor = UIColor.white
        title = "测试"
        
//        if #available(iOS 11, *){
//
//            navigationController?.navigationBar.prefersLargeTitles = true
//
//        }
        
        
    }
    
    func setupTable() -> Void {
        
       
        tableview = UITableView(frame: kScreenBounds, style: .plain)
         tableview?.delegate = self
         tableview?.dataSource = self
         tableview?.backgroundColor = UIColor.lightGray
        view.addSubview(tableview!)
        
        tableview?.tableFooterView = UIView()
        
        //注册单元格
        self.tableview?.register(UINib.init(nibName: "InfoCell", bundle: nil), forCellReuseIdentifier: iden)
        
    }
    
    
    
}


//MARK: -代理方法
extension ViewController{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return mutaArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: iden, for: indexPath) as! InfoCell
        
        cell.model = self.mutaArray[indexPath.row]
        
        cell.accessoryType = .disclosureIndicator
        
        cell.backgroundColor = UIColor.white
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 120
    
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
       tableView.deselectRow(at: indexPath, animated: true)
       
        let model = self.mutaArray[indexPath.row]
        
        let secVC = SecViewController()
        
        secVC.content = model
        
        self.navigationController?.pushViewController(secVC, animated: true)
        
    }
    
}


//MARK: -请求网络数据
extension ViewController{
    
    //请求网络数据
    func loadServerData() -> Void {
        
        ViewModel.loadData(a: a, type: type, page: page, maxString: maxTime) { (state, maxtime, array) in
            
           self.mutaArray += array
            
            self.tableview?.reloadData()
           
        }
    }
    
}

